package com.example.crowdfunding.mvc.Controller;

import com.example.crowdfunding.entity.test.Student;
import com.example.crowdfunding.utils.CrowdUtils;
import com.example.crowdfunding.utils.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 用来测试的controller类
 */
@Controller
@RequestMapping(value = "/test")
public class TestController {

    /**
     * The Logger.
     */
    Logger logger = LoggerFactory.getLogger(TestController.class);

    /**
     * 测试异常处理机制
     *
     * @param request the request
     * @return the string
     */
    @RequestMapping("/error.html")
    @ResponseBody
    public String testError(HttpServletRequest request) {
        // 测试工具类的方法：CrowdUtils.isAjaxRequest
        Boolean ajaxRequest = CrowdUtils.isAjaxRequest(request);
        logger.debug("是否为Ajax请求：" + ajaxRequest);
        // 错误 数学异常
        int i = 1 / 0;
        // 错误2 空指针
//        Student student = null;
//        Integer stuId = student.getStuId();
        // 错误3 数组越界
//        int[] numbs = new int[3];
//        int j = numbs[100];

        return "你还没有设置异常";
    }

    /**
     * 测试ajax请求发送复杂参数时的处理
     *
     * @param student the student
     * @param request the request
     * @return the string
     */
    @RequestMapping("/send/student.json")
    @ResponseBody
    public ResultEntity<Student> testReceiveComplexObject(@RequestBody Student student, HttpServletRequest request) {

        Boolean ajaxRequest = CrowdUtils.isAjaxRequest(request);

        return ResultEntity.successWithData(student, "是否为Ajax请求：" + ajaxRequest);
    }

}
